/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.commons.scripting.internal;

import java.util.List;

import net.zarubsys.commons.scripting.IScriptFunction;
import net.zarubsys.commons.scripting.ParsedStatement;
import net.zarubsys.commons.scripting.ScriptLocator;
import net.zarubsys.commons.scripting.ScriptingEnvironment;
import net.zarubsys.commons.scripting.ScriptingException;
import net.zarubsys.commons.scripting.utils.ScriptHelper;

/**
 * Based on scripting language from Media Solutions Slovakia, a.s.
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class FunctionStatement extends ParsedStatement {
	
	private final String functionName;
	private final List<ParsedStatement> args;
	
	public FunctionStatement(String functionName, ScriptLocator locator, List<ParsedStatement> args) {
		super(functionName, locator);
        this.functionName = functionName;
        this.args = args;
	}

	@Override
	public Object execute(ScriptingEnvironment env) throws ScriptingException {
		IScriptFunction function = ScriptHelper.getInstance().getFunction(functionName);
		if (function == null) {
			try {
				function = env.handleMissingFunction(functionName);
				if (function == null) {
					throw new IllegalArgumentException("Missing function handler just returned null! This should not happen.");
				}
			} catch (Exception e) {
				throw new ScriptingException("Error while searching for function '" + functionName + "'", e, getLocator());
			}
		}
		int argsSize = args.size();
		if ((argsSize < function.getMinArgsNum()) || ((function.getMaxArgsNum() != -1) && (argsSize > function.getMaxArgsNum()))) {
            throw new ScriptingException("Incorrect number of arguments (" + argsSize + ") to function '" + function.getName() + "'", getLocator());
		}
        env.setCurrentScriptExecutionLocator(getLocator());
		try {
            Object result = function.execute(env, args);
			return result;
        } catch (Exception e) {
            throw new ScriptingException("Error from function " + function.getName(), e, getLocator());
        }
	}

    @Override
	protected String getString() {
    	StringBuilder sb = new StringBuilder();
        sb.append(functionName);
        sb.append("(\n");
        for (ParsedStatement st : args) {
            st.toString();
            sb.append(",\n");
        }
        sb.append(')');
        return sb.toString();
    }

}
